/**
 * Project: TacticaPescaditos
 * File: Vector2D.java
 */
package org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.geometrics;

import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

/**
 * @author Carlos Alberto class: Vector2D
 */
public class Vector2D extends Point2D.Double{
	/**
	 * serialVersionUID:
	 */
	private static final long serialVersionUID = 1L;
	private static final double EPSILON = 0.1;

	/**
	 * @param x
	 * @param y
	 */
	public Vector2D(double x, double y) {
		super(x, y);

	}

	public Vector2D() {
		super(0,0);
	}

	public Vector2D substract(Point2D point2D) {
		Vector2D vector = new Vector2D();
		if (point2D != null) {
			vector.x = this.x - point2D.getX();
			vector.y = this.y -point2D.getY();
		}
		return vector;
	}

	public Vector2D normalized() {

		Vector2D vector = new Vector2D();

		double length = this.length();

		if (length > EPSILON){
			vector.x = this.x / length;
			vector.y = this.y / length;
		}
		return vector;
	}

	public double length() {
		return distance(0, 0);
	}
	

	public Vector2D multiply(double scalar) {
		Vector2D vector = new Vector2D();		
		vector.x = this.x*scalar;
		vector.y = this.y*scalar;
		return vector;
	}

	public double lengthSq() {
		return distanceSq(0, 0);
	}

	public Vector2D divide(double scalar) {
		Vector2D vector = new Vector2D();		
		vector.x = this.x/scalar;
		vector.y = this.y/scalar;
		return vector;
	}

	public Vector2D add(Vector2D other) {
		Vector2D vector = new Vector2D();
		vector.x = this.x + other.getX();
		vector.y = this.y + other.getY();
		return vector;
	}

	public Vector2D substract(double scalar) {
		Vector2D vector = new Vector2D();
		vector.x = this.x - scalar;
		vector.y = this.y - scalar;
		return vector;
	}

	public void reset() {
		this.x = 0;
		this.y = 0;
		
	}

	public Vector2D truncate(double max) {
		Vector2D vector = (Vector2D)this.clone();
		if (vector.length() > max) {
			vector = vector.normalized().multiply(max);			
		} 
		return vector;
	}

	public boolean isZero() {
		return dot(this)<EPSILON;
	}

	public double dot(Vector2D vector) {
		if (vector == null){
			return 0;
		}
		return this.x*vector.x+this.y*vector.y;
	}

	public Vector2D perp() {
		return new Vector2D(y, -x);
	}

	public Vector2D rotateAroundOrigin(double ang) {
		  Matrix2D mat = new Matrix2D();

		return  mat.rotate(ang).toVector2D(this);
	}

	
	
	/* (non-Javadoc)
	 * @see java.awt.geom.Point2D#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Vector2D) {
			double distanceSq = this.distanceSq((Vector2D)obj);		
			return  distanceSq < EPSILON;
		}
		return super.equals(obj);
	}

	public int sign(Vector2D v2) {
		  return (y*v2.x > x*v2.y) ? -1:1;		 
	}

	/* (non-Javadoc)
	 * @see java.awt.geom.Point2D#clone()
	 */
	@Override
	public Object clone() {
		return new Vector2D(this.x,this.y);
	}

	public Vector2D add(double scalar) {
		Vector2D vector = new Vector2D();
		vector.x = this.x + scalar;
		vector.y = this.y + scalar;
		return vector;
	}






	

}
